//
//  DKApplicationOrderDetailVc.m
//  DanKU
//
//  Created by EDY on 2023/11/9.
//

#import "DKApplicationOrderDetailVc.h"
#import "DKOrderDetailsCell.h"
#import "DKAppcationRepayTopView.h"
#import "DKRepayAgreementFootView.h"
#import "DKRepayLoanContract.h"

@interface  DKApplicationOrderDetailVc()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)DKOrderDetailModel *dataSource;

@end

@implementation DKApplicationOrderDetailVc

-(void)viewWillAppear:(BOOL)animated{
    [self makeNavBack];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =  DK_Hex(@"#FAFAFA");
    [self creatUI];
}

-(void)setOrderNo:(NSString *)orderNo{
    [DKPublicHttp apiGetOrderDetailAPI:@{@"orderNo":[NSString stringWithFormat:@"%@",orderNo],@"pageType":@"1"} complete:^(DKOrderDetailModel * _Nonnull orderlist) {
        self.dataSource = orderlist;
        [self.tableView reloadData];
        [self reportData];
    }];
}

-(void)reportData{
//    type一共有四种状态
//    1. AUDITING：审核中
//    2. ISSUING：放款中
//    3. ISSUED：放款成功
//    4. APPROVED：审核通过
    
    NSString *state = @"";
    switch (self.dataSource.status) {
        case 90:{
            state = @"AUDITING";
        }
            break;
        case 100:{
            state = @"APPROVED";
        }
            break;
        case 150:{
            state = @"ISSUING";
        }
            break;
        case 170:{
            state = @"ISSUED";
        }
            break;
            
        default:
            break;
    }
    
    if (state && state.length>0) {
        [DKPublicHttp apiUserBrhavRecordForAPI:@{@"type":state,@"loanOrderId":[NSString stringWithFormat:@"%@",self.dataSource.orderNo]} withPageName:DK_OrderStatus withAction:DK_ActionEnter complete:^{
                
        } fail:^{
                
        }];
    }
}

//-(void)setAppcationDetailTpye:(AppcationDetailTpye)appcationDetailTpye{
//    _appcationDetailTpye = appcationDetailTpye;
//}

-(void)creatUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(DK_SafeAreaNav_H);
    }];
}

-(UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = DK_Hex(@"#FAFAFA");
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[DKOrderDetailsCell class] forCellReuseIdentifier:@"DKOrderDetailsCell"];
        [_tableView registerClass:[DKRepayLoanContract class] forCellReuseIdentifier:@"DKRepayLoanContract"];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataSource.status != 170) {
        return  self.dataSource.pairs.count; // 根据数据源
    }else{
        return self.dataSource.pairs.count+1; // 放款成功
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSource.status != 170) {
        DKOrderDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DKOrderDetailsCell" forIndexPath:indexPath];
        cell.PairsSoure = self.dataSource.pairs[indexPath.row];
        return cell;
    }else{
        if (indexPath.row == self.dataSource.pairs.count) {// 数据个数
            DKRepayLoanContract *cell = [tableView dequeueReusableCellWithIdentifier:@"DKRepayLoanContract" forIndexPath:indexPath];
            return cell;
        }else{
            DKOrderDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DKOrderDetailsCell" forIndexPath:indexPath];
            cell.PairsSoure = self.dataSource.pairs[indexPath.row];
            return cell;
        }
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataSource.status == 170) {
        if (indexPath.row == self.dataSource.pairs.count) {// 数据源个数
            return 70;
        }
    }
    return 45;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 170;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    DKAppcationRepayTopView *headView = [[DKAppcationRepayTopView alloc] initWithFrame:CGRectMake(0, 0,DK_SCREEN_W , 170)];
    headView.dataSoure = self.dataSource;
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 150;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    DKRepayAgreementFootView *footView = [[DKRepayAgreementFootView alloc] initWithFrame:CGRectMake(0, 0,DK_SCREEN_W , 150)];
    footView.dataSource = self.dataSource;
    if (self.dataSource.status != 170) {
        footView.Type = 0;// 有数据根据类型区分
    }else{
        footView.Type = 1;// 有数据根据类型区分
    }
    return footView;
}


@end

